package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.dao.DishInfoMapper;
import org.example.dish_pojo.DishInfo;
import org.example.dish_pojo.DishTaste;
import org.example.dish_pojo.Result;
import org.example.service.IDishTasteService;
import org.example.dish_pojo.vo.QueryTaste;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品可选口味表 前端控制器
 * </p>
 *
 * @author mp
 * @since 2025-08-04
 */
@RestController
@RequestMapping("/dish-taste")
public class DishTasteController {

    @Autowired
    private IDishTasteService dishTasteService;
    @Autowired
    private DishInfoMapper dishInfoMapper;


    // 添加菜品可选口味
    @RequestMapping("/addDishTaste")
    public Result addDishTaste(@RequestBody DishTaste dishTaste) {
        boolean save = dishTasteService.save(dishTaste);
        if (!save) {
            return new Result(500, "添加失败", null);
        }
        return new Result(200, "添加成功", null);
    }


    // 删除菜品可选口味
    @RequestMapping("/deleteDishTaste")
    public Result deleteDishTaste(Integer tasteId) {
        boolean delete = dishTasteService.removeById(tasteId);
        if (!delete) {
            return new Result(500, "删除失败", null);
        }
        return new Result(200, "删除成功", null);
    }


    // 根据id回显数据
    @RequestMapping("/getTasteById")
    public Result getTasteById(@RequestParam("tasteId") Integer tasteId) {
        DishTaste taste = dishTasteService.getById(tasteId);
        return new Result(200, "操作成功", taste);
    }


    // 修改菜品可选口味
    @RequestMapping("/updateDishTaste")
    public Result updateDishTaste(@RequestBody DishTaste dishTaste) {
        boolean update = dishTasteService.updateById(dishTaste);
        if (!update) {
            return new Result(500, "修改失败", null);
        }
        return new Result(200, "修改成功", null);
    }


    // 分页查询菜品口味
    @RequestMapping("/listTaste")
    public Result listTaste(@RequestBody QueryTaste queryTaste) {
        Page<DishTaste> page = new Page<>(queryTaste.getCurrent(), queryTaste.getSize());
        QueryWrapper<DishTaste> wrapper = new QueryWrapper<>();
        wrapper.like(queryTaste.getTasteName() != null, "taste_name", queryTaste.getTasteName());
        dishTasteService.page(page, wrapper);
        List<DishTaste> list = page.getRecords();
        for (DishTaste d : list){
            Integer dishId = d.getDishId();
            DishInfo dishInfo = dishInfoMapper.selectById(dishId);
            d.setDname(dishInfo.getName());
        }
        return new Result(200, "查询成功", page);
    }


    // 根据dishId查询单条数据
    @RequestMapping("/tasteByDishId")
    public DishTaste tasteByDishId(@RequestParam("dishId") Integer dishId) {
        return dishTasteService.getOne(new QueryWrapper<DishTaste>().eq("dish_id", dishId));
    }



}

